﻿using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using log4net;
namespace AnyOffice.general.netdisk
{
    public partial class changefile : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["filename"] != null) ? Request.QueryString["filename"].ToString() : "";
                this.ViewState["FileName"] = str;
                NetdiskInfo netdiskInfo = NetdiskFactory.GetNetdiskInfo();
                if (netdiskInfo == null)
                {
                    Response.Redirect("index.aspx");
                }
                else if (!netdiskInfo.CanManage)
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    this.litNavTitle.Text = netdiskInfo.Name;
                    if (str == string.Empty)
                    {
                        this.pnlMain.Visible = false;
                        this.litMessage.Text = base.ShowPrompt("文件不存在", "netdisk.aspx");
                    }
                    else
                    {
                        string fileName = (netdiskInfo.DiskPath + str).Replace("/", @"\");
                        try
                        {
                            FileInfo info2 = new FileInfo(fileName);
                            if (info2.Exists)
                            {
                                this.txtFileName.Text = info2.Name;
                                this.ViewState["OldFileName"] = info2.Name;
                                this.litSize.Text = this.formatSize(info2.Length);
                                this.litCreationTime.Text = info2.CreationTime.ToString();
                                string str3 = info2.Directory.FullName.Replace(netdiskInfo.DiskPath, "").Replace(@"\", "/");
                                if (str3 == string.Empty)
                                {
                                    str3 = "/";
                                }
                                this.ViewState["parentPath"] = str3;
                                this.litParentPath.Text = str3;
                                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                            }
                            else
                            {
                                this.pnlMain.Visible = false;
                                this.litMessage.Text = base.ShowPrompt("文件不存在", "netdisk.aspx");
                            }
                        }
                        catch (Exception exception)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error("", exception);
                            }
                            this.pnlMain.Visible = false;
                            this.litMessage.Text = base.ShowPrompt("文件不存在", "netdisk.aspx");
                        }
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            if (this.txtFileName.Text == this.ViewState["OldFileName"].ToString())
            {
                Response.Redirect("netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
            }
            string str = "\\/:*?\"<>|";
            for (int i = 0; i < str.Length; i++)
            {
                if (this.txtFileName.Text.IndexOf(str.Substring(i, 1)) >= 0)
                {
                    this.litMessage.Text = base.ShowPrompt("文件名含有不合法字符(如\\/:*?\"<>|等)，请重新输入！", "changefile.aspx?filename=" + Server.UrlEncode(this.ViewState["FileName"].ToString()));
                    return;
                }
            }
            string str2 = (NetdiskFactory.GetNetdiskInfo().DiskPath + this.ViewState["parentPath"].ToString()).Replace("/", @"\");
            if (!str2.EndsWith(@"\"))
            {
                str2 = str2 + @"\";
            }
            try
            {
                if (File.Exists(str2 + this.txtFileName.Text))
                {
                    this.litMessage.Text = base.ShowError("修改文件属性出错：已有同名文件存在！", "netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
                    return;
                }
                File.Move(str2 + this.ViewState["OldFileName"].ToString(), str2 + this.txtFileName.Text);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
                this.litMessage.Text = base.ShowPrompt("修改文件属性失败！", "netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
                return;
            }
            Response.Redirect("netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
        }

        public string formatSize(long size)
        {
            if ((size > 0L) && (size < 1024L))
            {
                return "1k";
            }
            if (size < 1048576L)
            {
                return (Convert.ToString((long)(size / 1024L)) + "k");
            }
            float num = Convert.ToSingle(size) / 1048576f;
            return (num.ToString("0.0") + "M");
        }


    }
}
